<?php

    class BillController extends WebController
    {
        public $bill;
        
        public function init(){
            parent::init();
        }

        /**
        * @return array action filters
        */
        public function filters()
        {
            return array(
                'accessControl', // perform access control for CRUD operations
                'postOnly + delete', // we only allow deletion via POST request
            );
        }

        /**
        * Specifies the access control rules.
        * This method is used by the 'accessControl' filter.
        * @return array access control rules
        */
        public function accessRules()
        {
            return array(
                array('allow',
                    'actions'=>array('viewPrintForm'),
                    'users'=>array('*'),
                ),
                array('allow', // allow authenticated user to perform 'create' and 'update' actions
                    'actions'=>array('view', 'viewPrint'),
                    'expression' => array($this, 'ruleCheckView'),
                ),
                array('deny',  // deny all users
                    'users'=>array('*'),
                ),
            );
        }
        
        public function ruleCheckView($user){
            $code = Yii::app()->request->getQuery('code');
            $this->bill = $this->loadModel($code);
            if($this->manager && $this->manager->isStaff) return true;
            if($this->bill->customer_id == $user->id) return true;
            
            return false;            
        }


        public function actionView($code)
        {
            $model = $this->bill ? $this->bill : $this->loadModel($code);

            $info = json_decode($model->info, true);

            Item::model()->formatInfo($info);
            
            $this->pageTitle = "Đơn hàng {$model->code} | {$this->siteName}";
            $this->render('view',array(
                'model'=>$model,
                'info' => $info,
            ));
        }

        public function actionViewPrint($code)
        {
            $model = $this->bill ? $this->bill : $this->loadModel($code);

            $info = json_decode($model->info, true);

            Item::model()->formatInfo($info);
            $this->layout = '//layouts/root';
            
            $this->pageTitle = "Đơn hàng {$model->code} | {$this->siteName}";
            $this->render('view_print',array(
                'model'=>$model,
                'info' => $info,
            ));
        }
        
        public function actionViewPrintForm()
        {
            $this->layout = '//layouts/root';
            
            $this->pageTitle = "Đơn hàng | {$this->siteName}";
            $this->render('view_print_form');
        }

        /**
        * Returns the data model based on the primary key given in the GET variable.
        * If the data model is not found, an HTTP exception will be raised.
        * @param integer the ID of the model to be loaded
        */
        public function loadModel($code)
        {
            $model=Bill::model()->findByAttributes(array('code' => $code));
            if($model===null)
                throw new CHttpException(404,'The requested page does not exist.');
            return $model;
        }

    }
